package com.example.sevicedemo

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.work.WorkInfo
import androidx.work.WorkManager

class MainActivity : AppCompatActivity() {

    lateinit var tvResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvResult = findViewById(R.id.tvResult);
        findViewById<Button>(R.id.btn).setOnClickListener {

            //startService();
            // startIntentService();
            //startService02();
            //startJobService();
            //startJobIntentService()

            //SimpleWorker.startWorker(this)

            // startForegroundService01();

            startNotificationService();
        }

        findViewById<Button>(R.id.btnAIDL).setOnClickListener {
            bindServiceAIDL()
        }

        /*WorkManager.getInstance(this)
            .getWorkInfosByTagLiveData(TAG)
            .observe(this) {
                for (workInfo in it) {
                    if (workInfo.state == WorkInfo.State.SUCCEEDED) {
                        println("MainActivity.onCreate SUCCEEDED")
                    } else if (workInfo.state == WorkInfo.State.FAILED) {
                        println("MainActivity.onCreate FAILED")
                    }
                }

            }*/


    }

    private fun startNotificationService() {
        val intent = Intent(this, WeChatNotificationListenerService::class.java)
        startService(intent)
    }

    var mILoginAidlInterface: ILoginAidlInterface? = null
    private val mConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            mILoginAidlInterface = ILoginAidlInterface.Stub.asInterface(service)
            mILoginAidlInterface?.login()


        }

        override fun onServiceDisconnected(name: ComponentName?) {
            println("MainActivity.onServiceDisconnected")
        }

    }

    private fun bindServiceAIDL() {
        println("MainActivity.bindServiceAIDL")
        //客户端绑定服务端
        val intent = Intent();
        val aidlPackageName = "com.example.app2"; // 远程 aidl 的包名
        val aidlRemoteServiceName = "com.example.app2.AIDL2Service"; // 要绑定的远程服务名
        intent.component = ComponentName(aidlPackageName, aidlRemoteServiceName);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    }


    private fun startForegroundService01() {
        val intent = Intent(this, ForegroundService::class.java)
        startService(intent)
    }

    private fun startJobIntentService() {
        val intent = Intent()
        MyJobIntentService.enqueueWork(this, intent)
    }

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            println("MainActivity.onServiceConnected")
            val downloadBinder = service as MyService02.DownloadBinder
            downloadBinder.startDownload();

            downloadBinder.setLoginLinter(object : MyService02.DownloadBinder.LoginLinter {
                override fun loginLinter(loginStatus: Int, loginUser: String?) {
                    println("MainActivity.loginLinter")

                }

            })
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            println("MainActivity.onServiceDisconnected")
        }

    }

    private fun startJobService() {
        MyJobService.schedulePushWork(this)
    }

    private fun startService02() {
        val intent = Intent(this, MyService02::class.java)
        bindService(intent, connection, BIND_AUTO_CREATE)
    }

    private fun startIntentService() {
        val intent = Intent(this, MyIntentService::class.java)
        startService(intent)
    }

    private fun startService() {
        val intent = Intent(this, MyService01::class.java)
        startService(intent)
        //stopService(intent)
    }
}